package org.hljdrl.daf.protobuf;

import org.hljdrl.daf.test.ModuleTest;

import android.content.Context;

public class JavaToProtobufTest extends ModuleTest {

	private byte bytes[];
	public JavaToProtobufTest(Context ctx) {
		super(ctx);
		setName("Google-ProtoBuf");
	}

	@Override
	public String runEncode() {
		StringBuffer mSm = new StringBuffer();
		Data.Employee.Builder employee0 = Data.Employee.newBuilder();
		Data.Address.Builder address0 = Data.Address.newBuilder();
		employee0.setName("Mike");
		employee0.setAge(45);
		employee0.setSalary(20000.45);
		address0.setAddress1("Sagar");
		address0.setAddress2("Nerul");
		address0.setCity("Mumbai");
		address0.setZip(002345);
		employee0.setAddress(address0);
		
		Data.Employee emp = employee0.build();
		bytes = emp.toByteArray();
		mSm.append(formatBytes(bytes));
		return mSm.toString();
	}
	
	private String formatBytes(byte bytes[]){
		StringBuffer str = new StringBuffer();
		for(byte b:bytes){
			str.append(Integer.toBinaryString(b));
		}
		return str.toString();
	}

	@Override
	public String runDecode() {
		StringBuffer mSm = new StringBuffer();
		try {
			Data.Employee emp = Data.Employee.parseFrom(bytes);
			mSm.append(emp.toString());
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return mSm.toString();
	}

}
